/**
 * Filter organization list with permissions
 * @param {Array} data all organ list
 * @returns
 */
const getOrgan = data => {
  let organName,
    organId,
    organList = [],
    securityData

  for (let roleList of uni.getStorageSync('roleList')) {
    if (roleList.securityData) {
      securityData = roleList.securityData[0]
    }
  }

  if (securityData) {
    for (let pageItems of data) {
      if (securityData) {
        if (securityData.permissions.find(e => e == pageItems.organId)) {
          organList.push(pageItems)
        }
      }
    }
  }

  organName = organList.length > 0 ? organList[0].organName : uni.getStorageSync('extend').organName
  organId = organList.length > 0 ? organList[0].organId : uni.getStorageSync('extend').organId

  return { organId, organName, organList }
}

export { getOrgan }
